# -*- coding: utf-8 -*-
# !/usr/bin/env python
import datetime
import json
import time

from tair import Tair

from bot.enterprise_wechat_gpt.config.conf import *


def get_tair() -> Tair:
    tair: Tair = Tair(
        # host="r-8vbr8hx86gr7mi5qxw162.redis.zhangbei.rds.aliyuncs.com",
        host=profile_['redis']['host'],
        port=6379,
        db=profile_['redis']['db'],
        username="lingxi",
        password="Lingxi123#",
        decode_responses=True
    )
    return tair


class TairUtil:
    history_key = "QW:ConversationHistory:"

    def __init__(self):
        self.tair = get_tair()

    def add_conversation(self, user_id, msg):
        key_user_id = TairUtil.history_key + user_id
        self.tair.lpush(key_user_id, json.dumps(msg, ensure_ascii=False))
        # 只保留100条对话纪录
        if self.tair.llen(key_user_id) > 50:
            self.tair.rpop(key_user_id)
        if self.tair.ttl(key_user_id) == -1:
            # 7天过期
            self.tair.expire(key_user_id, 60 * 60 * 24 * 7)

    def get_conversation_history(self, user_id):
        conversation_list = self.tair.lrange(TairUtil.history_key + user_id, 0, -1)
        conversation_list.reverse()
        cl = list()
        for conversation in conversation_list:
            c = json.loads(conversation)
            cl.append(c)
        return cl

    def lrange(self, key, start, end):
        return self.tair.lrange(key, start, end)

    def flush_conversation(self, user_id):
        self.tair.delete(TairUtil.history_key + user_id)

    # def get_last_msg(self, user_id, index):
    #     return self.lindex(TairUtil.history_key + user_id, index)

    def lpush(self, key, *values):
        self.tair.lpush(key, *values)

    def rpop(self, key):
        return self.tair.rpop(key)

    def lpop(self, key):
        return self.tair.lpop(key)

    def lindex(self, key, index):
        return self.tair.lindex(key, index)

    def set(self, key, value):
        self.tair.set(key, value)

    def setnx(self, key, value):
        return self.tair.setnx(key, value)

    def get(self, key):
        return self.tair.get(key)

    def sadd(self, key, value):
        return self.tair.sadd(key, value)

    def srandmember(self, key):
        return self.tair.srandmember(key)

    def smembers(self, key):
        return self.tair.smembers(key)

    def is_member(self, key, value):
        return self.tair.sismember(key, value)

    def set_expire(self, key, v, expire=60):
        self.tair.setex(key, expire, v)

    def set_expire_second(self, key, expire=60):
        self.tair.expire(key, expire)

    def exists(self, key):
        return self.tair.exists(key)

    def srem(self, key, value):
        self.tair.srem(key, value)

    def incr(self, key):
        return self.tair.incr(key)

    def decr(self, key):
        return self.tair.decr(key)

    def set_if_absent(self, key, value):
        absent = self.tair.setnx(key, value)
        if absent:
            self.tair.expire(key, 60)
        return absent

    def keys(self, key):
        return self.tair.keys(key)

    def hset(self, key, field, value):
        self.tair.hset(key, field, value)

    def hget(self, key, field):
        return self.tair.hget(key, field)

    def hincr(self, key, field, amount=1):
        self.tair.hincrby(key, field, amount)

    def hsetnx(self, key, field, value, expire=30 * 24 * 3600):
        return self.tair.exhset(key, field, value, nx=True, ex=expire) == 1

    def exhget(self, key, field):
        return self.tair.exhget(key, field)

    def exhvals(self, key):
        return self.tair.exhvals(key)

    def exhkeys(self, key):
        return self.tair.exhkeys(key)


tair_util = TairUtil()


# 获取自然日的过期时间
def get_natural_day_expire_second(days):
    # 获取当前日期
    current_date = datetime.datetime.now().date()
    # 计算距离当天结束的时间差
    time_until = datetime.datetime.combine(current_date + datetime.timedelta(days=days), datetime.time.min) - datetime.datetime.now()
    return int(time_until.total_seconds())


if __name__ == "__main__":
    # print(len(tair_util.exhkeys("QW:CanActiveSendMsg")))
    print(tair_util.exhkeys("QW:CanActiveSendMsg"))


